package com.xxx.store.controller;

import com.xxx.store.controller.exception.*;
import com.xxx.store.entity.User;
import com.xxx.store.service.UserService;
import com.xxx.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @RequestMapping("regist")
    public JsonResult<Void> regist(User user){
        userService.regist(user);
        return new JsonResult<>(OK);
    }
    @RequestMapping("login")
    public JsonResult<User> login(String username, String password, HttpSession session){
        User login = userService.login(username, password);
        session.setAttribute("uid",login.getUid());
        session.setAttribute("username",login.getUsername());

        System.out.println("用户id："+getUidFromSession(session)+"密码："+getUsernameFromSession(session));
        return new JsonResult<User>(OK,login);
    }
    @RequestMapping("change_password")
    public JsonResult<Void> changePassword(String oldPassword,String newPassword,HttpSession session){
        Integer uid=getUidFromSession(session);
        String username = getUsernameFromSession(session);
        userService.changePassword(uid,username,oldPassword,newPassword);
        return new JsonResult<Void>(OK);
    }
    @RequestMapping("getInfo")
    public JsonResult<User> getInfo(HttpSession session){
        User user = userService.getByUid(getUidFromSession(session));
        return new JsonResult<>(OK, user);
    }
    @RequestMapping("changeInfo")
    public JsonResult<Void> changeInfo(HttpSession session,User user){
        userService.changeInfo(getUidFromSession(session),getUsernameFromSession(session),user);
        return new JsonResult<>(OK);
    }
    public static final int AVATAR_SIZE=10*1024*1024;
    public static final List<String> AVATAR_TYPE=new ArrayList<>();
    static {
        AVATAR_TYPE.add("image/png");
        AVATAR_TYPE.add("image/jpeg");
        AVATAR_TYPE.add("image/gif");
        AVATAR_TYPE.add("image/bmp");
    }
    @RequestMapping("changeAvatar")
    public JsonResult<String> changeAvatar(HttpSession session, MultipartFile file){
        if (file.isEmpty()){
            throw new FileEmptyException("文件为空");
        }
        if (file.getSize()>AVATAR_SIZE){
            throw new FileSizeException("文件大小超出限制");
        }
        if (!AVATAR_TYPE.contains(file.getContentType())){
            throw new FileTypeException("文件类型不支持");
        }
        String upload = "E:\\JavaCode\\store\\src\\main\\resources\\static\\upload";
        File dir = new File(upload);
        if (!dir.exists()){
            dir.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename=UUID.randomUUID().toString()+suffix;
        File dest = new File(dir,filename);//空文件
        try {
            file.transferTo(dest);
        }catch (FileStateException e){
            throw new FileStateException("文件状态异常");
        } catch (IOException e) {
            throw new FileUploadException("文件读写异常");
        }
        String avatar="/upload/"+filename;
        userService.changeAvatar(getUidFromSession(session),avatar,getUsernameFromSession(session));
        return new JsonResult<>(OK,avatar);
    }
}
